package top.hcode.hoj.dao;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.mapper.OrgClassMapper;
import top.hcode.hoj.pojo.entity.org.OrgClass;

/**
 * @author orangej
 * @since 2025-02-14
 */
@Service
@RequiredArgsConstructor
public class OrgClassDao extends ServiceImpl<OrgClassMapper, OrgClass> {
    private final LazyDao dao;

    /**
     * 重建班级信息表
     */
    @Transactional
    public void rebuildOrgClass() {
        dao.runSql("truncate table org_class");

        dao.runSql("insert into org_class (id, tree_code, class_name, class_num, grade_id, grade_num)\n" +
                   "select id, tree_code, org_name, class_num, parent_id, grade_num from org where org_type = 7;");
        dao.runSql("update org_class t0\n" +
                   "left join org t1 on substr(t0.tree_code, 1, 4) = t1.tree_code\n" +
                   "set t0.school_id = t1.id\n" +
                   "where t1.id is not null;");
        dao.runSql("update org_class t0\n" +
                   "left join org t1 on substr(t0.tree_code, 1, 8) = t1.tree_code\n" +
                   "set t0.campus_id = t1.id\n" +
                   "where t1.id is not null;");
    }
}
